<template>
  <div>
    <data-table :head="thead3" :data="certificate" :page='currentPage1' :actions="['edit','del']"
                @callback="usermanipulate"
                class="intendance-table"></data-table>
    <i class="el-icon-circle-plus-outline add-icon" @click="addUser"></i>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                   :current-page.sync="currentPage1" layout="total,prev, pager, next, jumper"
                   :total="totalnum2" background>
    </el-pagination>
  </div>
</template>

<script>
  import DataTable from '@/components/common/DataTable.vue'
  import dictService from '@/services/DictionaryService.js'

  export default {
    props: ['certificate', 'totalnum2'],
    name: 'UserCertificate',
    components: {
      'data-table': DataTable
    },
    data () {
      return {
        totalPage2: 1,
        currentPage1: 1,
        thead3: [{ name: "证书类型", prop: "name" }],
        tableData3: [],
        useroptions: []
      }
    },
    methods: {
      usermanipulate (param) {
        if (param.action == 'edit') {
          this.$prompt('请输入证书类型', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
            inputErrorMessage: '证书类型不能为空',
            inputValue: param.data.name,
            customClass: 'customClass',
          }).then(({ value }) => {
            let obj = {
              code: param.data.code, // 数据代码 ,
              extData: param.data.extData, // 附加存储 ,
              laws: param.data.laws, // 法律法规、典型案例 ,
              name: value, // 数据名称 ,
              orderx: param.data.orderx, // 显示顺序 ,
              remark: param.data.remark, // 数据描述 ,
              createTime: param.data.createTime,
              id: param.data.id,
            }
            dictService.update(obj).then(res => {

              this.$emit('update')
              this.$message({
                type: 'success',
                message: '修改成功'
              })
            })
          }).catch(() => {
            this.$message('您已取消此次编辑')
          })
        } else if (param.action == 'del') {
          dictService.delete(param.data.id).then(res => {

            this.$emit('update')
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          })
        }
      },
      addUser () {
        this.$prompt('请输入证书类型', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
          inputErrorMessage: '证书类型不能为空',
          customClass: 'customClass',
        }).then(({value}) => {
          let obj = {
            code: 'ZSLX', // 数据代码 ,
            extData: null, // 附加存储 ,
            laws: null, // 法律法规、典型案例 ,
            name: value, // 数据名称 ,
            orderx: null, // 显示顺序 ,
            remark: null, // 数据描述 ,
          }
          dictService.create(obj).then(res => {

            this.$emit('update')
            this.$message({
              type: 'success',
              message: '创建成功'
            })
          })
        }).catch(() => {
          this.$message('您已取消此次添加')
        })
      },
      handleSizeChange (val) {
        this.parent.getData(val-1)
      },
      handleCurrentChange (val) {
        this.parent.getData(val-1)
      },
    }
  }
</script>

<style scoped lang="scss" src="../Dcustom.scss"></style>
